package org.langqiao.experiment.experiment6;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ChangeNum extends JFrame implements ActionListener{
	private static final String labelName[]={"十进制","二进制","八进制","十六进制"};
	private JLabel jl[]=new JLabel[labelName.length];
	private JTextField jtf[]=new JTextField[labelName.length+1];
	private JComboBox jcb;	
	private static int flag=0;//标记是否出错 0无错 1出错
	
	ChangeNum(){
		super("进制转换");
		this.setBounds(300, 200, 300, 250);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		JPanel jp1=new JPanel(new GridLayout(5,1));
		JPanel jp2=new JPanel(new GridLayout(5,1,5,5));
		
		this.add(jp1);
		this.add(jp2);
	
		jcb=new JComboBox(labelName);
		jtf[0]=new JTextField(15);
		jtf[0].addActionListener(this);
		jp1.add(jcb);
		jp2.add(jtf[0]);
		for(int i=0;i<labelName.length;i++){
			jl[i]=new JLabel(labelName[i]);
			jp1.add(jl[i]);
			jtf[i+1]=new JTextField(15);
			jp2.add(jtf[i+1]);
		}
		JLabel l=new JLabel("选择下拉框中的进制并输入");
		l.setForeground(Color.pink);
		JLabel l1=new JLabel("相应的数字后按Enter键转化");
		l1.setForeground(Color.pink);
		this.add(l);
		this.add(l1);
		this.setVisible(true);
		
	}
	//转化为十进制数
	public int toDec(String text){
		int num=0;
		if(jcb.getSelectedItem().toString().equals("十进制")){
			try{
				num=Integer.parseInt(text);
			}catch(NumberFormatException nfe){
				flag=1;
				JOptionPane.showMessageDialog(this,text+"不能转化为整数");
			}
		}
		if(jcb.getSelectedItem().toString().equals("二进制")){
			int n=0;
			for(int i=text.length()-1,j=0;i>=0;i--,j++){
				try{
					n=Integer.parseInt(Character.toString(text.charAt(i)));
				}catch(NumberFormatException nfe){
					flag=1;
					JOptionPane.showMessageDialog(this,text+"不能转化为整数！");
				}
				if(n>1){
					flag=1;
					JOptionPane.showMessageDialog(this,text+"不是二进制数！");
				}
				num+=(int)Math.pow(2,j)*n;
			}
		}
		if(jcb.getSelectedItem().toString().equals("八进制")){
			int n=0;
			for(int i=text.length()-1,j=0;i>=0;i--,j++){
				try{
					n=Integer.parseInt(Character.toString(text.charAt(i)));
				}catch(NumberFormatException nfe){
					flag=1;
					JOptionPane.showMessageDialog(this,text+"不能转化为整数！");
				}
				if(n>8){
					flag=1;
					JOptionPane.showMessageDialog(this,text+"不是八进制数！");
				}
				num+=(int)Math.pow(8,i)*n;
			}
		}
		if(jcb.getSelectedItem().toString().equals("十六进制")){
			int n=0;
			text=text.toLowerCase();
			for(int i=text.length()-1,j=0;i>=0;i--,j++){
				try{
					if(text.charAt(i)>='0'&&text.charAt(i)<='9')
						n=Integer.parseInt(Character.toString(text.charAt(i)));
					else if(text.charAt(i)>='a'&&text.charAt(i)<='f')
						n=10+text.charAt(i)-'a';
					else {
						flag=1;
						JOptionPane.showMessageDialog(this,text+"不是十六进制数！");
					}
				}catch(NumberFormatException nfe){
					flag=1;
					JOptionPane.showMessageDialog(this,text+"不能转化为整数！");
				}
				num+=(int)Math.pow(16,i)*n;
			}
		}
		return num;
	}
	//将十进制数n转化为m进制数
	public String convert(int n,int m){
		String str="";
		if(n<m){
			if(m>10) 
				str+=(char)('a'+(n-10));
			else
				str+=n;
		}
		
		else{
			while(n>0){
				str=n%m+str;
				n=n/m;
			}
		}
		return str;
	}
	
	
	public static void main(String []args){
		new ChangeNum();
	}

	public void actionPerformed(ActionEvent e) {
		String text=jtf[0].getText();
		int num=this.toDec(text);
		if(flag==0){
			jtf[1].setText(Integer.toString(num));
			jtf[2].setText(this.convert(num, 2));
			jtf[3].setText(this.convert(num, 8));
			jtf[4].setText(this.convert(num, 16));
		}
		//输入出错时各项都设置为空
		else{
			jtf[0].setText("");
			jtf[1].setText("");
			jtf[2].setText("");
			jtf[3].setText("");
			jtf[4].setText("");
		}
	}
}
